package de.tabuma.formcreator.xml.logik;

import de.tabuma.formcreator.entity.*;

import java.io.*;
import java.util.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

import java.util.logging.*;

/**
 * Handler for integrating soap messages.
 * @author $Author: $
 * @version $Revision: $
 * @since $Date: $
 */
public class SoapHandler implements org.xml.sax.ContentHandler {

    private static final Logger log = Logger.getLogger(SoapHandler.class.getName());

    String url;
    Vector<FormStructur> objectList;
    Class className;

    public SoapHandler(String content) {
        try {
            objectList = new Vector<FormStructur>();
            XMLReader parser = XMLReaderFactory.createXMLReader();
            parser.setContentHandler(this);
            InputSource myInputSource = new InputSource(new StringReader(content));
            parser.parse(myInputSource);
        }
        catch(Exception e) {
            log.warning(e.getMessage());
            String warning = "";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i].toString()+"\n";
            }
            log.warning(warning);
        }
    }

    private FormStructur parserState;
    public FormStructur toFormStructur() throws ClassCastException {
        return (FormStructur) parserState;
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        String str = String.valueOf(ch).substring(start, start+length);
        str = str.replaceAll("\t", "");
        str = str.replaceAll("\n", "");
        str = str.replaceAll("  ", "");
        if(str.length()>1) {
            log.fine("added "+str+" to "+parserState.getName());
            parserState.setValue(str);
        }
    }

    public void startDocument() throws SAXException {
        log.finest("Start parsing");
    }

    public void endDocument() throws SAXException {
        log.finest("End parsing");
    }

    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        if(this.parserState==null) {
            parserState = new FormStructur(localName, "", 1, 1, false, false, false, false);
            objectList.add(parserState);
            log.fine(this.getWhiteSpace()+"<"+localName+" uri=\""+uri+"\">");
        }
        else {
            FormStructur fs = new FormStructur(localName, "", 1, 1, false, false, false, false);
            parserState.addFormularStruktur(fs);
            this.parserState = fs;
            objectList.add(parserState);
            log.fine(this.getWhiteSpace()+"<"+localName+" uri=\""+uri+"\" head=\""+objectList.get(objectList.size()-2).getClass().getSimpleName()+"\">");
        }
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        log.fine(this.getWhiteSpace()+"</"+localName+" class=\""+parserState.getClass().getSimpleName()+"\">");
        if(this.objectList.size()>1) {
            objectList.removeElementAt(this.objectList.size()-1);
            this.parserState = objectList.elementAt(this.objectList.size()-1);
        }
    }

    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
        //
    }

    public void processingInstruction(String target, String data) throws SAXException {
        //
    }

    public void setDocumentLocator(Locator locator) {
        //
    }

    public void skippedEntity(String name) throws SAXException {
        //
    }

    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        log.finest("start prefix mapping:"+prefix);
    }

    public void endPrefixMapping(String prefix) throws SAXException {
        log.finest("end prefix mapping:"+prefix);
    }

    public String getWhiteSpace() {
        String str = "";
        for(int i=0;i<this.objectList.size();i++) {
            str += "   ";
        }
        return str;
    }
}